
import { _decorator, Component, Game, VERSION, sys, view, Label } from 'cc';
import { App } from '../src/App';
import { EventConst } from '../src/core/Common';
const { ccclass, property } = _decorator;
 
@ccclass('LaunchScene')
export class LaunchScene extends Component {
    @property({type:Label,tooltip:'纹理占用内存'})
    lblText:Label = null;
    onLoad () {
        this.node.on(Game.EVENT_HIDE, () => {
            App.Event.emit(EventConst.APP_HIDE);
        });
        this.node.on(Game.EVENT_SHOW, () => {
            App.Event.emit(EventConst.APP_SHOW);
        });
        this.node.on(Game.EVENT_RESTART, () => {
        });
        sys.isBrowser && view.setResizeCallback(()=>{
            App.Event.emit(EventConst.RESIZE_VIEWPORT);
        });

        let isDebug = true;
        App.init(this.node,'config',(is:boolean)=>{
            isDebug = App.getValue('debug');
            this.lblText.node.active = isDebug;
        });

        // 异常捕获
        const mainVer = parseInt(VERSION.split('.')[0]);
        if (mainVer < 3){
            // @ts-ignore
            window.__errorHandler = (file: any, line: any, errorMessage:any, error: any)=>{
                const exception = {
                    file:file,
                    line:line,
                    errorMessage:errorMessage,
                    error:error
                };
                const exce = JSON.stringify(exception);
                console.log('<=======>', exce);
                isDebug && App.showMessageBox(App.Util.format(App.i18n.getString('comMsgBox'),exce));
            }
        } else {
            const consoleError = window.console.error;
            window.console.error = ()=>{
                console.log('<=======>', JSON.stringify(arguments));
                consoleError && consoleError.apply(window, arguments);
                isDebug && App.showMessageBox(App.Util.format(App.i18n.getString('comMsgBox'),JSON.stringify(arguments)));
            }
        }
    }

    onEnable () {
        App.Audio.keepPlay();
    }

    update(){
        if (this.lblText.node.active){
            const { count, memory } = App.Asset.getTextureUsage();
            this.lblText.string = `纹理数量:${count}  |  内存:${memory.toFixed(2)} M`;
        }
    }
}
